package com.project.service.impl;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.bean.OutboundBean;
import com.project.bean.StockBean;
import com.project.mapper.IOutboundMapper;
import com.project.mapper.IStockMapper;
import com.project.service.IOutboundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class OutboundServiceImpl implements IOutboundService {
    @Autowired
    private IStockMapper stockMapper;
    @Autowired
    private IOutboundMapper outboundMapper;

    @Override
    public List<OutboundBean> findById(String name) {//
        QueryWrapper<OutboundBean> qw = new QueryWrapper<>();
        qw.eq("o_userName", name);
        List<OutboundBean> list = outboundMapper.selectList(qw);//根据销售员姓名（登录用户的session）查询销售车集合，
        for (int i = 0; i < list.size(); i++) {
            OutboundBean outboundBean = list.get(i);
            StockBean stockBean = stockMapper.selectById(outboundBean.getStockId());//找到销售车中的商品
            outboundBean.setStockBean(stockBean);
        }
        return list;
    }

    @Override
    public void add(String id,String name) { // 将需要出库的商品添加到销售车
        //判断销售车中是否有添加的商品
        QueryWrapper<OutboundBean> qw=new QueryWrapper<>();
        qw.eq("o_userName",name);
        qw.eq("fk_stockId",id);
        OutboundBean outbound=outboundMapper.selectOne(qw);
        if (outbound!=null) {
            //销售车中的商品加一
            int num=1;
            outbound.setNum(outbound.getNum()+num);
            outboundMapper.updateById(outbound);//更新
        }else {
            //销售车新增商品
            outboundMapper.insert(new OutboundBean(id,name));
        }
    }

    @Override
    public void remove(Integer id) {//移除销售车中的商品
        outboundMapper.deleteById(id);

    }

    @Override
    public void updateNum(Integer id, Integer newNum) {//修改销售商品的数量
        OutboundBean outbound=outboundMapper.selectById(id);
        outbound.setNum(newNum);
        outboundMapper.updateById(outbound);
    }
}
